﻿using DataAccess.FinancialLeasingLoan;
using FOModels.FinancialLeasingLoan;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Bussiness.FinancialLeasingLoan
{
    public class FinancialLeasingLoanBLL
    {
        private FinancialLeasingLoanDAL dal;
        public FinancialLeasingLoanBLL()
        {
            dal = new FinancialLeasingLoanDAL();
        }
        public List<FinancialLeasingLoanApplyModels> getcontract()
        {
            List<FinancialLeasingLoanApplyModels> list = new List<FinancialLeasingLoanApplyModels>();
            FinancialLeasingLoanApplyModels fur;
            DataTable tb = dal.getcontract();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new FinancialLeasingLoanApplyModels();
                    fur.FinancialLeasingLoanApplyID = int.Parse(row[0].ToString());
                    fur.FinancialLeasingLoanID = int.Parse(row[1].ToString());
                    fur.CustomerName = row[2].ToString();
                    fur.CustomerEmail = row[3].ToString();
                    fur.CustomerPhone = row[4].ToString();
                    fur.CustomerIncome = row[5].ToString();
                    fur.CustomerDOB = row[6].ToString();
                    fur.StatusID = int.Parse(row[7].ToString());
                    fur.AppDate = row[8].ToString();
                    fur.FinancialLeasingLoanName = row[10].ToString();
                    fur.StatusName = row[17].ToString();
                    fur.CompanyImg = row[19].ToString();
                    fur.CompanyName = row[26].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<FinancialLeasingLoanApplyModels> getcontractbystatuscount(int statusid)
        {
            List<FinancialLeasingLoanApplyModels> list = new List<FinancialLeasingLoanApplyModels>();
            FinancialLeasingLoanApplyModels fur;
            DataTable tb = dal.getcontractbystatuscount(statusid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new FinancialLeasingLoanApplyModels();
                    fur.FinancialLeasingLoanApplyID = int.Parse(row[0].ToString());
                    fur.FinancialLeasingLoanID = int.Parse(row[1].ToString());
                    fur.CustomerName = row[2].ToString();
                    fur.CustomerEmail = row[3].ToString();
                    fur.CustomerPhone = row[4].ToString();
                    fur.CustomerIncome = row[5].ToString();
                    fur.CustomerDOB = row[6].ToString();
                    fur.StatusID = int.Parse(row[7].ToString());
                    fur.AppDate = row[8].ToString();
                    fur.FinancialLeasingLoanName = row[10].ToString();
                    fur.StatusName = row[17].ToString();
                    fur.CompanyImg = row[19].ToString();
                    fur.CompanyName = row[26].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public FinancialLeasingLoanApplyModels getcontractbyid(int FinancialLeasingLoanapplyid)
        {

            FinancialLeasingLoanApplyModels fur = new FinancialLeasingLoanApplyModels();
            DataTable tb = dal.getcontractbyid(FinancialLeasingLoanapplyid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new FinancialLeasingLoanApplyModels();
                    fur.FinancialLeasingLoanApplyID = int.Parse(row[0].ToString());
                    fur.FinancialLeasingLoanID = int.Parse(row[1].ToString());
                    fur.CustomerName = row[2].ToString();
                    fur.CustomerEmail = row[3].ToString();
                    fur.CustomerPhone = row[4].ToString();
                    fur.CustomerIncome = row[5].ToString();
                    fur.CustomerDOB = row[6].ToString();
                    fur.StatusID = int.Parse(row[7].ToString());
                    fur.AppDate = row[8].ToString();
                    fur.FinancialLeasingLoanName = row[10].ToString();
                    fur.StatusName = row[17].ToString();
                    fur.CompanyImg = row[19].ToString();
                    fur.CompanyName = row[26].ToString();

                }
            }
            return fur;
        }
        public int FinancialLeasingLoanApply(int FinancialLeasingLoanID, FinancialLeasingLoanApplyModels FinancialLeasingLoan)
        {
            if (dal.FinancialLeasingLoanApply(FinancialLeasingLoanID, FinancialLeasingLoan) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int DeleteFinancialLeasingLoanApply(int FinancialLeasingLoanApplyID)
        {
            if (dal.DeleteFinancialLeasingLoanApply(FinancialLeasingLoanApplyID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int UpdateStatus(FinancialLeasingLoanApplyModels FinancialLeasingLoan)
        {
            if (dal.UpdateStatus(FinancialLeasingLoan) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public FinancialLeasingLoanModels getTOP1()
        {

            FinancialLeasingLoanModels fur = new FinancialLeasingLoanModels();
            DataTable tb = dal.getTOP1();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {

                    fur.FinancialLeasingLoanID = int.Parse(row[0].ToString());
                }
                return fur;
            }
            else
                return null;
        }
        public List<FinancialLeasingLoanModels> getall(int langid)
        {
            List<FinancialLeasingLoanModels> list = new List<FinancialLeasingLoanModels>();
            FinancialLeasingLoanModels fur;
            DataTable tb = dal.getall(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {

                    fur = new FinancialLeasingLoanModels();
                    fur.FinancialLeasingLoanID = int.Parse(row[0].ToString());
                    fur.FinancialLeasingLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = row[3].ToString();
                    fur.MaximumBalance = row[4].ToString();
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.FlagImage = row[27].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public FinancialLeasingLoanModels getFinancialLeasingLoanByID(int FinancialLeasingLoanID, int langid)
        {

            FinancialLeasingLoanModels fur = new FinancialLeasingLoanModels();
            DataTable tb = dal.getFinancialLeasingLoanByID(FinancialLeasingLoanID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.FinancialLeasingLoanID = int.Parse(row[0].ToString());
                    fur.FinancialLeasingLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                }
                return fur;
            }
            else
                return null;
        }
        public FinancialLeasingLoanModels getFinancialLeasingLoanByIDManager(int FinancialLeasingLoanID, int langid)
        {

            FinancialLeasingLoanModels fur = new FinancialLeasingLoanModels();
            DataTable tb = dal.getFinancialLeasingLoanByIDManager(FinancialLeasingLoanID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.FinancialLeasingLoanID = int.Parse(row[0].ToString());
                    fur.FinancialLeasingLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                }
                return fur;
            }
            else
                return null;
        }
        public List<FinancialLeasingLoanModels> getFinancialLeasingLoanDefault(int langid)
        {
            List<FinancialLeasingLoanModels> list = new List<FinancialLeasingLoanModels>();
            FinancialLeasingLoanModels fur;
            DataTable tb = dal.getFinancialLeasingLoanDefault(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new FinancialLeasingLoanModels();
                    fur.FinancialLeasingLoanID = int.Parse(row[0].ToString());
                    fur.FinancialLeasingLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<FinancialLeasingLoanModels> getFinancialLeasingLoanrelati(int CompanyID, int HomeLoanID, int langid)
        {
            List<FinancialLeasingLoanModels> list = new List<FinancialLeasingLoanModels>();
            FinancialLeasingLoanModels fur;
            DataTable tb = dal.getFinancialLeasingLoanrelati(CompanyID, HomeLoanID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new FinancialLeasingLoanModels();
                    fur.FinancialLeasingLoanID = int.Parse(row[0].ToString());
                    fur.FinancialLeasingLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<FinancialLeasingLoanModels> getFinancialLeasingLoan(int planid, string balance, int years, int langid)
        {
            List<FinancialLeasingLoanModels> list = new List<FinancialLeasingLoanModels>();
            FinancialLeasingLoanModels fur;
            DataTable tb = dal.getFinancialLeasingLoan(planid, balance, years, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new FinancialLeasingLoanModels();
                    fur.FinancialLeasingLoanID = int.Parse(row[0].ToString());
                    fur.FinancialLeasingLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<FinancialLeasingLoanModels> getFinancialLeasingLoanByProvider(int planid, string balance, int years, int CompanyID, int langid)
        {
            List<FinancialLeasingLoanModels> list = new List<FinancialLeasingLoanModels>();
            FinancialLeasingLoanModels fur;
            DataTable tb = dal.getFinancialLeasingLoanByProvider(planid, balance, years, CompanyID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new FinancialLeasingLoanModels();
                    fur.FinancialLeasingLoanID = int.Parse(row[0].ToString());
                    fur.FinancialLeasingLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<FinancialLeasingLoanModels> getFinancialLeasingLoanDefaultByProvider(int CompanyID,int langid)
        {
            List<FinancialLeasingLoanModels> list = new List<FinancialLeasingLoanModels>();
            FinancialLeasingLoanModels fur;
            DataTable tb = dal.getFinancialLeasingLoanDefaultByProvider(CompanyID,langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new FinancialLeasingLoanModels();
                    fur.FinancialLeasingLoanID = int.Parse(row[0].ToString());
                    fur.FinancialLeasingLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public int UpdateFinancialLeasingLoan(FinancialLeasingLoanModels models, int langid)
        {
            if (dal.UpdateFinancialLeasingLoan(models) > 0 && dal.UpdateFinancialLeasingLoanLang(models, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertFinancialLeasingLoan(FinancialLeasingLoanModels models)
        {
            if (dal.InsertFinancialLeasingLoan(models) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertFinancialLeasingLoanLang(FinancialLeasingLoanModels models, int langid)
        {
            if (dal.InsertFinancialLeasingLoanLang(models, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int DeleteFinancialLeasingLoan(int CarLoanID)
        {
            if (dal.DeleteFinancialLeasingLoanLang(CarLoanID) > 0 && dal.DeleteFinancialLeasingLoan(CarLoanID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
    }
}
